package com.google.code.jmarias.client.components;

import com.google.code.jmarias.client.Entity;
import com.google.code.jmarias.client.Globals;
import com.google.code.jmarias.client.GameLocals;
import com.google.code.jmarias.common.Card;
import java.util.ArrayList;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.StateBasedGame;

/**
 * Pokusi se setridit karty, nejdriv podle barev, pak podle hodnot.
 * V budoucnu bude muze mit uzivatel i vic modu radeni.
 * 
 * @author bart
 */
public class InitNSortCardsComponent extends AbstractComponent {
     public InitNSortCardsComponent(int id, Globals global, GameLocals table, ArrayList<Card> cardsObjects, Vector2f [] cardsPositions) throws SlickException {
        this.id = id;

        ArrayList<Card>sortedCards = new ArrayList<Card>();

        System.out.println("Tridim karty ..");

        sortedCards.add(cardsObjects.get(0));

        for(int i = 1; i < cardsObjects.size(); i++) {
            for(int j = 0; j < sortedCards.size(); j++) {
                if(cardsObjects.get(i).toOrdinal() < sortedCards.get(j).toOrdinal()) {
                    sortedCards.add(j, cardsObjects.get(i));
                    break;
                } else if(j == sortedCards.size() - 1) {
                    sortedCards.add(cardsObjects.get(i));
                    break;
                }
            }
        }



        for(int i = 0; i < cardsObjects.size(); i++) {
            System.out.print(cardsObjects.get(i).toOrdinal() + " ");
        }

        System.out.println("Setrizeno: ");

        for(int i = 0; i < sortedCards.size(); i++) {
            System.out.print(sortedCards.get(i).toOrdinal() + " ");
        }


        table.getCards().clear();
        table.getCards().addAll(sortedCards);


        // Vytvoreni entit karet, nastaveni jejich pozice a meritka
        table.getCardsEntities().clear();
        for(int i = 0; i < table.getCards().size(); i++) {
            table.getCardsEntities().add(new Entity("card" + table.getCards().get(i).toOrdinal(), i, table.getCards().get(i)));
            table.getCardsEntities().get(i).AddComponent(new ImageRenderComponent(0, new Image(global.getIMGPATH() + "karty/" + table.getCards().get(i).toOrdinal() + ".gif")));
            table.getCardsEntities().get(i).setPosition(cardsPositions[0]);
            table.getCardsEntities().get(i).setScale(table.getHandScale());
        }

    }

    @Override
    public void update(GameContainer gc, StateBasedGame sb, int delta) {
        System.out.println("Inicializace karet");
        owner.DelComponent(id);
    }


}
